<template>
	<view class="container">
		<u-loading-page :loading="isLoading" loading-text="加载中......" loading-mode="semicircle"></u-loading-page>
		<view class="search-bar">
			<u-search placeholder="请输入图书名称" v-model="queryParam.keyWords" :clearabled="true"
				@custom="handleSearch"></u-search>
		</view>
		<scroll-view class="content-body" scroll-y style="height: 100%;" @scrolltolower="onScrollToLower">
			<view class="content-body-item" v-for="(book, index) in bookList" :key="book.id">
				<view class="content-body-item-photo" @click="handleBookDetail(book.id)">
					<image v-if="book.photo" :src="book.photo" class="book-photo"></image>
					<view v-else class="no-photo">暂无图片</view>
				</view>
				<view class="content-body-item-text" @click="handleBookDetail(book.id)">
					<text class="book-name">{{ book.name }}</text>
					<text class="book-author">作者：{{ book.author }}</text>
					<text class="book-press">出版社：{{ book.press }}</text>
				</view>
			</view>
			<u-loadmore :status="hasMore ? 'loadmore' : 'nomore'" nomoreText="没有更多了" />
		</scroll-view>
	</view>
</template>

<script>
	import {
		newTotalBookList,
		bookList,
		secondCategorysList,
		recommendTotalbookList
	} from '@/api/book';

	export default {
		data() {
			return {
				isLoading: false,
				bookList: [],
				keyWords: '',
				total: 0,
				queryParam: {
					pageNum: 1,
					pageSize: 20,
					keyWords: '',
					classifyId: null,
					classifyIds: ''
				},
				hasMore: true,
			};
		},
		onLoad(options) {
			console.log('能拿到不', options);
			this.type = options.type;
			this.queryParam.keyWords = options.keyword;
			this.queryParam.classifyId = options.classifyId;
			this.queryParam.classifyIds = options.classifyIds;
			this.getList();
		},
		methods: {
			async getList() {
				if (this.isLoading || !this.hasMore) return;
				this.isLoading = true;
				let res;
				if (this.type == 0) {
					res = await bookList(this.queryParam);
				} else if (this.type == 1) {
					res = await newTotalBookList(this.queryParam);
				} else if (this.type == 2) {
					res = await recommendTotalbookList();
				} else if (this.type == 3) {
					res = await bookList(this.queryParam);
				} else if (this.type == 4) {
					res = await secondCategorysList(this.queryParam);
				}
				if (res.code == 200) {
					this.bookList = [...this.bookList, ...res.rows];
					this.total = res.total;
					this.hasMore = this.bookList.length < this.total;
					this.queryParam.pageNum++;
					this.isLoading = false;
				}
			},
			onScrollToLower() {
				console.log('触发滚动到底部事件');
				this.getList();
			},
			handleBookDetail(id) {
				uni.navigateTo({
					url: `/book/book-detail/index?bookId=${id}`,
				});
			},
			handleSearch() {
				this.queryParam.pageNum = 1;
				this.bookList = [];
				this.hasMore = true;
				this.getList();
			},
		},
		mounted() {
			this.getList();
		},
	};
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f5f5;
	}

	.search-bar {
		width: 90%;
		margin: 10px auto;
	}

	.content-body {
		flex: 1;
		overflow-y: auto;
	}

	.content-body-item {
		display: flex;
		background-color: #ffffff;
		margin: 10px 0;
		padding: 10px;
		border-radius: 5px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.content-body-item-photo {
		width: 80px;
		height: 100px;
		border-radius: 5px;
		overflow: hidden;
		background-color: #e0e0e0;
	}

	.book-photo {
		width: 100%;
		height: 100%;
	}

	.no-photo {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
		color: #999;
		font-size: 12px;
	}

	.content-body-item-text {
		flex: 1;
		margin-left: 10px;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.book-name {
		font-size: 16px;
		font-weight: bold;
	}

	.book-author,
	.book-press {
		font-size: 14px;
		color: #666;
	}
</style>